import axios from 'axios';
axios.defaults.baseURL = "https://hq.techgp.cn/rjhy-gmg-quote/api/1/",//"/mapi/";
axios.defaults.timeout = 5000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
const instance = axios.create({
    headers: { 'Content-type': 'multipart/form-data' },
    timeout: 5000,
});
//请求拦截
axios.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        return Promise.error(error);
    })
//响应拦截器  
axios.interceptors.response.use(
    response => {
        return Promise.resolve(response);
    },
    error => {
        return Promise.error(error);
    })

export function get(url) {
    return new Promise((resolve, reject) => {
        axios.get(url).then(res => {
            //console.log(res)
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    })
}

export function post(url) {
    return new Promise((resolve, reject) => {
        axios.post(url,{},{emulateJSON:true} )
            .then(res => {
                    resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}

export function postform(url, data) {
    return new Promise((resolve, reject) => {
        instance.post(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err.data)
            })
    });
}
/*jsonp*/
export function jsonp(url) {
    if (!url) {
        return;
    }
    return new Promise((resolve, reject) => {
        window.jsonCallBack = (result) => {
            resolve(result)
        }
        var JSONP = document.createElement("script");
        JSONP.type = "text/javascript";
        JSONP.src = `${url}&callback=jsonCallBack`;
        document.getElementsByTagName("head")[0].appendChild(JSONP);
        setTimeout(() => {
            document.getElementsByTagName("head")[0].removeChild(JSONP)
        }, 500)
    })
}
